package com.leon.datalink.core.persistence;

import org.junit.Test;
import static org.junit.Assert.*;

/**
 * 简单的持久化功能测试
 */
public class SimplePersistenceTest {

    @Test
    public void testPersistenceConfigManager() {
        // 设置测试属性
        System.setProperty("datalink.persistence.enabled", "true");
        System.setProperty("datalink.persistence.storage.path", "target/test-persistence");
        
        // 获取配置管理器实例
        PersistenceConfigManager manager = PersistenceConfigManager.getInstance();
        assertNotNull("PersistenceConfigManager should not be null", manager);
        
        // 测试全局持久化开关
        assertTrue("Global persistence should be enabled", manager.isGlobalPersistenceEnabled());
        
        // 测试Actor类型持久化配置
        manager.setActorTypePersistence("java.lang.String", true);
        assertTrue("String class should be configured for persistence",
                  manager.shouldPersist("java.lang.String", "test-node", null));
        
        // 测试消息持久化判断
        boolean shouldPersist = manager.shouldPersistMessage("test-node", "TestMessage");
        assertTrue("Test message should be persisted by default", shouldPersist);
        
        // 清理测试属性
        System.clearProperty("datalink.persistence.enabled");
        System.clearProperty("datalink.persistence.storage.path");
    }
    
    @Test
    public void testPersistenceDisabled() {
        // 设置持久化为禁用
        System.setProperty("datalink.persistence.enabled", "false");
        
        PersistenceConfigManager manager = PersistenceConfigManager.getInstance();
        
        // 测试全局持久化开关
        assertFalse("Global persistence should be disabled", manager.isGlobalPersistenceEnabled());
        
        // 测试消息持久化判断
        boolean shouldPersist = manager.shouldPersistMessage("test-node", "TestMessage");
        assertFalse("Messages should not be persisted when globally disabled", shouldPersist);
        
        // 清理测试属性
        System.clearProperty("datalink.persistence.enabled");
    }
    
    @Test
    public void testMessageBlacklist() {
        // 设置持久化为启用
        System.setProperty("datalink.persistence.enabled", "true");
        System.setProperty("datalink.persistence.message.blacklist", "HeartbeatMessage,PingMessage");
        
        PersistenceConfigManager manager = PersistenceConfigManager.getInstance();
        
        // 测试黑名单消息
        assertFalse("HeartbeatMessage should be blacklisted", 
                   manager.shouldPersistMessage("test-node", "HeartbeatMessage"));
        assertFalse("PingMessage should be blacklisted", 
                   manager.shouldPersistMessage("test-node", "PingMessage"));
        
        // 测试非黑名单消息
        assertTrue("DataMessage should not be blacklisted", 
                  manager.shouldPersistMessage("test-node", "DataMessage"));
        
        // 清理测试属性
        System.clearProperty("datalink.persistence.enabled");
        System.clearProperty("datalink.persistence.message.blacklist");
    }
}
